package com.gree.flowable.app.tools.utils;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimestampTypeAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp> {
    private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public TimestampTypeAdapter() {
    }

    @Override
    public JsonElement serialize(Timestamp ts, Type t, JsonSerializationContext jsc) {
        String dfString = this.format.format(new Date(ts.getTime()));
        return new JsonPrimitive(dfString);
    }

    @Override
    public Timestamp deserialize(JsonElement json, Type t, JsonDeserializationContext jsc) throws JsonParseException {
        if (!(json instanceof JsonPrimitive)) {
            throw new JsonParseException("The date should be a string value");
        } else {
            try {
                Date date = this.format.parse(json.getAsString());
                return new Timestamp(date.getTime());
            } catch (ParseException var5) {
                throw new JsonParseException(var5);
            }
        }
    }
}